package com.ipt.frm.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class EjemploAjax
 */
public class EjemploAjax extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EjemploAjax() {
		super();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		PrintWriter out = resp.getWriter();

		resp.setContentType("text/html");
		resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());

		out.print("Hola " + req.getParameter("nombre"));

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		response.setContentType("text/html");
		response.setCharacterEncoding(StandardCharsets.UTF_8.toString());

		if (null != request.getParameter("nombre")) {
			out.print("Hola Mundo");
		} else {
			out.print("Hola " + request.getParameter("nombre"));
		}

	}

}
